package com.msda.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msda.pojo.dto.PageDTO;
import com.msda.pojo.dto.TokenDTO;
import com.msda.pojo.dto.staff.*;
import com.msda.pojo.entity.requirement.CustomerApproval;
import com.msda.pojo.entity.staff.Customer;
import com.msda.pojo.entity.staff.CustomerDetail;
import com.msda.pojo.entity.staff.User;
import com.msda.service.staff.ApprovalService;
import com.msda.service.staff.UserService;
import com.msda.utils.Result;
import com.msda.pojo.vo.AllUserMsg;
import com.msda.pojo.vo.UserLoginVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.zip.DataFormatException;

@CrossOrigin
@RestController
@RequestMapping("/user")
@Validated
@Tag(name = "用户控制器", description = "对用户进行增删改查已经登录等操作")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private ApprovalService approvalService;

    @Operation(summary = "管理员添加用户", description = "检查用户信息合法性，通过后添加用户")
    @PostMapping("/add_user")
    public Result<String> register(@RequestBody UserDTO userDTO)throws IOException {
        return Result.success(userService.userRegister(userDTO));
    }

    @Operation(summary = "客户注册")
    @PostMapping("/customer/register")
    public Result<String> registerCustomer(@RequestBody Customer customer){
        return Result.success(approvalService.customerRegister(customer));
    }

    @Operation(summary = "根据用户Id查询", description = "检查用户信息合法性，通过后查询用户")
    @PostMapping("/get_user/{userId}")
    public Result<?> getUser(@PathVariable String userId){
        return Result.success(userService.getUser(userId));
    }

    @Operation(summary = "注销账户")
    @PostMapping("/delete_user")
    public Result<?> deleteUser(@RequestBody UserIdDTO userIdDTO){
        return Result.success(userService.deleteUser(userIdDTO.getUserIdList()));
    }

    @Operation(summary = "更新用户")
    @PostMapping("/update_user")
    public Result<?> updateUser(@RequestBody UserDTO userDTO) throws DataFormatException {
        return Result.success(userService.updateUser(userDTO));
    }

    @Operation(summary = "登录功能")
    @PostMapping("/login_user")
    public Result<UserLoginVO> loginUser(@RequestBody UserLoginDTO userLoginDTO) {
        return Result.success(userService.login(userLoginDTO));
    }

    @Operation(summary = "自动登录功能")
    @PostMapping("/auto_login_user")
    public Result<?> autoLoginUser(@RequestBody TokenDTO tokenDTO) {
        return Result.success(userService.autoLogin(tokenDTO));
    }

    @Operation(summary = "获取所有员工条数",description = "管理员使用获取所有员工条数")
    @PostMapping("/get_all_staff_count")
    public Result<Long> getAllStaffCount() {

        return Result.success(userService.getAllStaffCount());
    }

    @Operation(summary = "获取所有客户条数",description = "管理员使用获取所有用户条数")
    @PostMapping("/get_all_customer_count")
    public Result<Long> getAllCustomerCount() {

        return Result.success(userService.getAllCustomerCount());
    }

    @Operation(summary = "分页获取所有员工",description = "管理员使用获取所有员工条数")
    @PostMapping("/get_all_staff_page")
    public Result<List<User>> getAllStaffPage(@RequestBody PageDTO pageDTO) {

        return Result.success(userService.getAllStaffPage(pageDTO));
    }

    @Operation(summary = "分页获取所有客户")
    @PostMapping("/get_all_customer_page")
    public Result<List<Customer>> getAllCustomerPage(@RequestBody PageDTO pageDTO) {

        return Result.success(userService.getAllCustomerPage(pageDTO));
    }


    @Operation(summary = "注册状态信息查询",description = "根据账号查询注册表的申请信息")
    @PostMapping("/customer/inquire/{account}")
    public Result<?> inquireState(@PathVariable String account) {
        return Result.success(approvalService.inquireState(account));
    }

    @Operation(summary = "注册申请撤销",description = "客户取消注册申请")
    @PostMapping("/customer/quash_register/{account}")
    public Result<Integer> quashRegister(@PathVariable String account) {
        return Result.success(approvalService.quashRegister(account));
    }



    @Operation(summary = "管理员根据状态获取申请表的所有申请数据条数")
    @PostMapping("/admin/get_all_approval_count/{status}")
    public Result<Long> getAllApproval(@PathVariable int status) {
        return Result.success(approvalService.getAllApproval(status));
    }

    @Operation(summary = "管理员根据状态分页获取申请表申请数据")
    @PostMapping("/admin/get_page_approval/{status}")
    public Result<List<CustomerApproval>> getPageApproval(@PathVariable int status, @RequestBody PageDTO pageDTO) {
        return Result.success(approvalService.getPageApproval(status,pageDTO));
    }

    @Operation(summary = "管理员修改注册状态表信息")
    @PostMapping("/admin/audit_approval")
    public Result<Integer> inquireState(@RequestBody AuditApprovalDTO auditApprovalDTO) {
        return Result.success(approvalService.auditApproval(auditApprovalDTO));
    }

    @Operation(summary = "管理员冻结或解冻用户")
    @PostMapping("/admin/frozen_user")
    public Result<?> frozenState(@RequestBody FrozenUserDTO frozenUserDTO) {
        return Result.success(userService.frozenUser(frozenUserDTO));
    }

    @Operation(summary = "重置密码")
    @PostMapping("/user_reset_password")
    public Result<?> userResetPassword(@RequestBody ResetPasswordDTO ResetPasswordDTO) {
        return Result.success(userService.userResetPassword(ResetPasswordDTO));
    }

    @Operation(summary = "设置与修改头像功能")
    @PostMapping("/set_profilePicture")
    public Result<?> set_profilePicture(@RequestParam("id") String id, @RequestParam("image") MultipartFile image) throws IOException {
        return Result.success(userService.set_profilePicture(id,image));
    }



}
